<template>
  <div id="activeManagementSearch">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label="人审结果">
          <el-select v-model="formInline.manualVerify" clearable placeholder="">
            <el-option
              v-for="item in checkStatuList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机审结果">
          <el-select v-model="formInline.autoVerify" clearable placeholder="">
            <el-option
              v-for="item in machineStatuList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序规则">
          <el-select v-model="formInline.orderWay" placeholder="请选择">
            <el-option
              v-for="(item, index) in orderWayList"
              :key="'item' + index"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="timeRule">
            <el-option
              v-for="(item, index) in timeTypeList"
              :key="'item' + index"
              :label="item.key"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-date-picker
            v-model="createTime"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="标题查询">
          <el-input
            clearable
            v-model="formInline.content"
            placeholder="请输入标题查询"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户搜索" prop="userValue">
          <el-input
            v-model="formInline.userValue"
            placeholder="请输入相应的搜索内容"
          >
            <el-select
              slot="prepend"
              v-model="formInline.userType"
              placeholder="请选择"
            >
              <el-option label="大鹏号" value="DAPENGACCOUNT"></el-option>
            </el-select>
          </el-input>
        </el-form-item>
        <el-form-item label="审核人" prop="verifyUserId">
          <el-select
            clearable
            v-model="formInline.verifyUserId"
            placeholder=""
            filterable
          >
            <el-option
              v-for="(item, index) in personList"
              :key="'item' + index"
              :label="item.nickname"
              :value="item.dapengUserId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="展示方式" prop="showType">
          <el-select v-model="formInline.showType" clearable placeholder="">
            <el-option
              v-for="item in displayRuleList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择学院" prop="college">
          <el-select v-model="formInline.college" clearable placeholder="">
            <el-option
              v-for="item in belongCollegeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户属性" prop="userProperty">
          <el-select v-model="formInline.userProperty" clearable>
            <el-option
              v-for="item in userAttList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
import { getCreateTime } from '@utils/pubFunction'
import { getDpNum } from '@/api/system/user'

export default {
  name: 'ActiveManagementSearch',
  msg: '阅读内容-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          manualVerify: '', // 机审结果
          autoVerify: '', // 人审结果
          orderWay: '',
          content: '',
          userType: '',
          userValue: '',
          verifyUserId: '', // 审核人
          showType: '', // 展示方式
          startTime: '',
          endTime: '',
          pubStartTime: '',
          pubEndTime: '',
          userProperty: '', // 用户属性
          college: ''
        }
      }
    }
  },
  data() {
    return {
      // 人审结果
      checkStatuList: [{ dictLabel: '全部', dictValue: '' }],
      // 机审结果
      autoVerifyList: [
        {
          name: '全部',
          value: ''
        },
        {
          name: '审核通过',
          value: 'PASS'
        },
        {
          name: '审核未通过',
          value: 'REJECT'
        }
      ],
      machineStatuList: [{ dictLabel: '全部', dictValue: '' }],
      userAttList: [{ dictLabel: '全部', dictValue: '' }], // 用户属性
      // 排序
      orderWayList: [],
      timeTypeList: [
        { key: '创建时间', value: 'ADD' },
        { key: '发布时间', value: 'PUB' }
      ],
      createTime: [],
      timeRule: 'PUB',
      // 审核人
      personList: [{ nickname: '全部', dapengUserId: '' }],
      // 展示方式
      displayRuleList: [{ dictLabel: '全部', dictValue: '' }],
      // 用户搜索
      searchList: [{ key: '大鹏号', value: 'DAPENGACCOUNT' }],
      // 选择学院
      belongCollegeList: [{ dictLabel: '全部', dictValue: '' }]
    }
  },
  mounted() {
    this.getStatusList()
    this.getCheckUser()
  },
  methods: {
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      //设置默认数据
      this.setDefaultValue()
    },
    async getStatusList() {
      // 人审结果
      let data = await sysNormalDisable('reads_audit_status', 'readAuditStatus')
      this.checkStatuList = this.checkStatuList.concat(data)
      // 机审结果
      let machine = await sysNormalDisable('reads_machine_audit_status')
      this.machineStatuList = this.machineStatuList.concat(machine)
      // 排序规则
      this.orderWayList = await sysNormalDisable('read_content_list_sort')
      let rule = await sysNormalDisable('read_open_type', 'showType')
      this.displayRuleList = this.displayRuleList.concat(rule)
      // 获取评论库筛选条件-学院
      let data2 = await sysNormalDisable('sys_share_college', 'readCollegeType')
      this.belongCollegeList = this.belongCollegeList.concat(data2)
      // 用户属性
      let UserAttr = await sysNormalDisable(
        'audit_user_property3',
        'readConUserAttr'
      )
      this.userAttList = this.userAttList.concat(UserAttr)
      this.setDefaultValue()
    },
    /*设置默认数据*/
    setDefaultValue() {
      this.formInline.manualVerify = this.checkStatuList[0].dictValue
      this.formInline.autoVerify = this.machineStatuList[0].dictValue
      this.formInline.orderWay = this.orderWayList[0].dictValue
      this.formInline.userType = this.searchList[0].value
      this.formInline.content = ''
      this.timeRule = 'PUB'
      this.createTime = []
    },
    // 审核人
    getCheckUser() {
      getDpNum({ type: 'OPERATORS' }, (response) => {
        this.personList = this.personList.concat(response)
      })
    }
  },
  watch: {
    'formInline.orderWay': {
      handler(newVal, oldVal) {
        if (newVal.startsWith('ADD')) {
          this.timeRule = 'ADD'
        } else {
          this.timeRule = 'PUB'
        }
      },
      deep: true
    },
    timeRule: {
      handler(newVal, oldVal) {
        this.formInline.startTime = ''
        this.formInline.endTime = ''
        this.formInline.pubStartTime = ''
        this.formInline.pubEndTime = ''
        const createTime = this.createTime
        if (newVal === 'ADD') {
          this.formInline.startTime = createTime[0]
          this.formInline.endTime = createTime[1]
        } else if (newVal === 'PUB') {
          this.formInline.pubStartTime = createTime[0]
          this.formInline.pubEndTime = createTime[1]
        } else {
          return
        }
      }
    },
    createTime: {
      handler(newVal, oldVal) {
        this.formInline.startTime = ''
        this.formInline.endTime = ''
        this.formInline.pubStartTime = ''
        this.formInline.pubEndTime = ''
        if (newVal && newVal.length > 1) {
          if (this.timeRule === 'ADD') {
            this.formInline.startTime = newVal[0]
            this.formInline.endTime = newVal[1]
          } else if (this.timeRule === 'PUB') {
            this.formInline.pubStartTime = newVal[0]
            this.formInline.pubEndTime = newVal[1]
          } else {
            return
          }
        }
      },
      deep: true
    }
  }
}
</script>

<style lang="less">
#activeManagementSearch {
  .el-input {
    &.el-input--suffix {
      width: 182px !important;
    }
  }
}
</style>
